
package icu.d4peng.basic.knife4j.model;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.github.xiaoymin.knife4j.core.extend.OpenApiExtendMarkdownChildren;
import com.github.xiaoymin.knife4j.core.util.CommonUtils;

/**
 * @author d4peng d4peng@qq.com
 * @date 2023-11-25 19:42:44
 */
public class MarkdownFiles {

	private static final Logger LOGGER = LoggerFactory.getLogger(MarkdownFiles.class);

	private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();

	/***
	 * markdown files dir
	 */
	private String basePath;

	private List<OpenApiExtendMarkdownChildren> markdownFiles = new ArrayList<>();

	public MarkdownFiles() {
	}

	public MarkdownFiles(String basePath) {
		this.basePath = basePath;
	}

	public List<OpenApiExtendMarkdownChildren> getMarkdownFiles() {
		return markdownFiles;
	}

	public void setMarkdownFiles(List<OpenApiExtendMarkdownChildren> markdownFiles) {
		this.markdownFiles = markdownFiles;
	}

	public String getBasePath() {
		return basePath;
	}

	public void setBasePath(String basePath) {
		this.basePath = basePath;
	}

	public void init() {
		// 初始化
		if (basePath != null && basePath != "" && !"".equals(basePath)) {
			try {
				Resource[] resources = resourceResolver.getResources(basePath);
				if (resources != null && resources.length > 0) {
					for (Resource resource : resources) {
						OpenApiExtendMarkdownChildren markdownFile = createMarkdownFile(resource);
						if (markdownFile != null) {
							getMarkdownFiles().add(markdownFile);
						}
					}
				}
			}
			catch (Exception e) {
				LOGGER.warn("(Ignores) Failed to read Markdown files,Error Message:{} ", e.getMessage());
			}
		}
	}

	private OpenApiExtendMarkdownChildren createMarkdownFile(Resource resource) {
		OpenApiExtendMarkdownChildren markdownFile = new OpenApiExtendMarkdownChildren();
		if (resource != null) {
			LOGGER.info(resource.getFilename());
			// 只读取md
			if (resource.getFilename().toLowerCase().endsWith(".md")) {
				BufferedReader reader = null;
				try {
					reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), "UTF-8"));
					String le = null;
					String title = resource.getFilename();
					String reg = "#{1,3}\\s{1}(.*)";
					Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
					Matcher matcher = null;
					while ((le = reader.readLine()) != null) {
						// 判断line是否是包含标题
						matcher = pattern.matcher(le);
						if (matcher.matches()) {
							title = matcher.group(1);
						}
						break;
					}
					CommonUtils.close(reader);

					markdownFile.setTitle(title);
					markdownFile.setContent(new String(CommonUtils.readBytes(resource.getInputStream()), "UTF-8"));
					return markdownFile;
				}
				catch (Exception e) {
					LOGGER.warn("(Ignores) Failed to read Markdown files,Error Message:{} ", e.getMessage());
				}
				finally {
					CommonUtils.close(reader);
				}
			}
		}
		return null;
	}

}
